package com.ainge.easystudy.thread;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author: Ainge
 * @Time: 2021/6/11 01:58
 */
public class ThreadPoolExecutorDemo {

    private static AtomicInteger integer =  new AtomicInteger();

    public static void main(String[] args) {

        ThreadPoolExecutor service = new ThreadPoolExecutor(2, 10, 1L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), (r, executor) -> {
            System.out.println(String.format("进入自定义拒绝策略,r=%s,executor=%s", r, executor));
            // 转为提交者进行执行
            r.run();
        });

        for (int i = 0; i < 100; i++) {
            service.execute(() -> {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException ignored) {
                }
                integer.incrementAndGet();
                System.out.println(Thread.currentThread().getName() + "\t" + "working...");
            });
        }

        try {
            TimeUnit.SECONDS.sleep(20);
            System.out.println(service);
            System.out.println(integer.get());
            service.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
